package com.springboot.project.common.controller;

import com.springboot.config.annotion.BusinessLog;
import com.springboot.config.exception.BussinessException;
import com.springboot.config.tool.PageUtils;
import com.springboot.config.tool.Query;
import com.springboot.config.tool.Tip;
import com.springboot.project.common.dao.ModuleDao;
import com.springboot.project.common.dao.UserDao;
import com.springboot.project.common.entity.Dept;
import com.springboot.project.common.entity.Role;
import com.springboot.project.common.node.ZTreeNode;
import com.springboot.project.common.service.impl.DeptServiceImpl;
import com.springboot.project.common.service.impl.RoleServiceImpl;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author lby
 * @since 2018-05-06
 */
@Controller
@RequestMapping("/role")
public class RoleController {

    private static String PREFIX = "system/role/";

    @Autowired
    RoleServiceImpl roleService;

    @Autowired
    DeptServiceImpl deptService;

    @Autowired
    ModuleDao moduleDao;

    @Autowired
    UserDao userDao;

    /**
     * 跳转到查看管理员列表的页面
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "role";
    }

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ResponseBody
    PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        Query query = new Query(params);
        List<Map<String, Object>> sysRoleList = roleService.roleTableList(query);
        int total = roleService.count(query);
        PageUtils pageUtil = new PageUtils(sysRoleList, total);
        return pageUtil;
    }

    /**
     * 跳转到权限设置弹出框
     * @param roleId
     * @param model
     * @return
     */
    @RequestMapping(value = "/setUpPower/{roleId}")
    public String roleAssign(@PathVariable("roleId") Integer roleId, Model model) {
        model.addAttribute("roleId", roleId);
        return PREFIX + "/role_power";
    }

    /**
     * 跳转到添加页面
     */
    @RequestMapping("/add")
    public String addView() {
        return PREFIX + "role_add";
    }

    /**
     * 跳转到编辑角色页面
     */
    @RequestMapping("/edit/{id}")
    public String userEdit(@PathVariable Integer id, Model model) {
        Role role = roleService.selectById(id);
        model.addAttribute("role",role);

        //根据角色id获取角色名
        int pRoleId = role.getParentid();
        if(pRoleId == 0) {
            model.addAttribute("pRoleName", "顶级");
        }else
        {
            Role pRole = roleService.selectById(role.getParentid());
            model.addAttribute("pRoleName", pRole.getName());
        }
        //部门
        int pDeptid = role.getDeptid();
        if(pDeptid == 0)
        {
            model.addAttribute("deptName", "顶级");
        }else {
            Dept dept = deptService.selectById(role.getDeptid());
            if (dept != null) {
                model.addAttribute("deptName", dept.getFullname());
            }
        }
        return PREFIX + "role_edit";
    }

    /**
     * 删除角色
     */
    @RequestMapping("/remove/{id}")
    @BusinessLog(value = "删除角色")
    @RequiresPermissions("/role/del")
    @ResponseBody
    public Tip remove(@PathVariable Integer id) {
        if(id == 1)
        {
            throw new BussinessException(500, "不能删除超级管理员！");
        }
        moduleDao.deleteByRoleId(id);
        userDao.delByRoleId(id);
        roleService.deleteById(id);
        return new Tip();
    }

    /**
     * 配置权限
     */
    @RequestMapping("/setAuthority")
    @BusinessLog(value = "角色权限分配")
    @RequiresPermissions("/role/setUpPower")
    @ResponseBody
    public Tip setAuthority(@RequestParam("roleId") Integer roleId, @RequestParam("ids") String ids) {
        roleService.setAuthority(roleId, ids);
        return new Tip();
    }

    /**
     * 添加用户
     */
    @RequestMapping("/addRole")
    @BusinessLog(value = "添加角色")
    @RequiresPermissions("/role/add")
    @ResponseBody
    public Tip addUser(Role role) {
        roleService.insert(role);
        return new Tip();
    }

    /**
     * 编辑用户
     */
    @RequestMapping("/editRole")
    @BusinessLog(value = "修改角色")
    @RequiresPermissions("/role/edit")
    @ResponseBody
    public Tip editRole(Role role) {
        roleService.updateById(role);
        return new Tip();
    }

    /**
     * 判断用户名是否已经存在
     */
    @RequestMapping("/exit")
    @ResponseBody
    public boolean exit(@RequestParam Map<String, Object> params) {
        // 存在，不通过，false
        return !roleService.exit(params);
    }

    /**
     * 获取角色树
     */
    @RequestMapping("/roleTree")
    @ResponseBody
    public List<ZTreeNode> roleTree() {
        return roleService.roleTree();
    }
}
